﻿using Autofac;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintRecord;

namespace ZoneLine.Printers
{
    public class WLQCPrinter : IPrinter
    {
        public string PrintLabel(string printTemplatePath, string printerName, int index)
        {
            var (paras, code) = GetPara(index);

            PrinterHelper.PrinterLabel(printTemplatePath, printerName, paras);
            return code;
        }

        private (Dictionary<string, string>, string) GetPara(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var lxConfig = config.WlqcConfig;
            var paras = new Dictionary<string, string>();
            paras.Add("LJH_N5", lxConfig.PartNo);
            paras.Add("BBH_N5", lxConfig.VersionNo);
            var productStr = GetProductData();
            paras.Add("SCRQ_N5", productStr);
            paras.Add("GYSDM_N5", lxConfig.SupplierCode);
            paras.Add("SCQFBSXX_N5", lxConfig.ProductionInfo);

            var serialNo = GetSerial(index);
            paras.Add("XLH_N5", serialNo);
            var code = $"[>16{lxConfig.PartNo}V{lxConfig.VersionNo}D{productStr}{lxConfig.SupplierCode}S{lxConfig.ProductionInfo}N{serialNo}";
            paras.Add("EWM_N5", code);
            WLRecord wLRecord = new WLRecord();
            wLRecord.SerialNumber = index;
            wLRecord.Code = code;
            wLRecord.PartNo = lxConfig.PartNo;
            wLRecord.ProductionInfo = lxConfig.ProductionInfo;
            wLRecord.SerialNo = lxConfig.SerialNo;
            wLRecord.SupplierCode = lxConfig.SupplierCode;
            wLRecord.TemplatePath = lxConfig.TemplatePath;
            wLRecord.TemplatePathR = lxConfig.TemplatePathR;
            wLRecord.VersionNo = lxConfig.VersionNo;
            DBContext.DbConfig.DbContext.Insert<WLRecord>(wLRecord).ExecuteInserted();
            return (paras, code);
        }

        public void PrintLabelShow(string path, string printerName, int index)
        {
            var (paras, code) = GetPara(index);

            PrinterHelper.PrinterLabelShow(path, printerName, paras);
        }

        private string GetProductData()
        {
            var days = DateTime.Now.DayOfYear.ToString("000");
            var years = DateTime.Now.Year.ToString().Substring(2);
            return $"{days}{years}";
        }

        private string GetSerial(int index)
        {
            return index.ToString("00000");
        }
    }
}